using Microsoft.EntityFrameworkCore;
using AdminSystem.API.Data;
using AdminSystem.API.DTOs;
using AdminSystem.API.Models;
using AutoMapper;

namespace AdminSystem.API.Services
{
    public class ContentService : IContentService
    {
        private readonly ApplicationDbContext _context;
        private readonly IMapper _mapper;

        public ContentService(ApplicationDbContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        public async Task<List<ContentDto>> GetAllContentsAsync()
        {
            var contents = await _context.Contents
                .Include(c => c.Author)
                .OrderByDescending(c => c.CreatedAt)
                .ToListAsync();

            return _mapper.Map<List<ContentDto>>(contents);
        }

        public async Task<ContentDto?> GetContentByIdAsync(int id)
        {
            var content = await _context.Contents
                .Include(c => c.Author)
                .FirstOrDefaultAsync(c => c.Id == id);

            return content == null ? null : _mapper.Map<ContentDto>(content);
        }

        public async Task<ContentDto> CreateContentAsync(CreateContentDto createContentDto, int authorId)
        {
            var content = new Content
            {
                Title = createContentDto.Title,
                Body = createContentDto.Body,
                ContentType = createContentDto.ContentType,
                AuthorId = authorId,
                IsPublished = createContentDto.IsPublished,
                CreatedAt = DateTime.UtcNow,
                PublishedAt = createContentDto.IsPublished ? DateTime.UtcNow : null
            };

            _context.Contents.Add(content);
            await _context.SaveChangesAsync();

            return await GetContentByIdAsync(content.Id) ?? throw new InvalidOperationException("Failed to retrieve created content");
        }

        public async Task<ContentDto?> UpdateContentAsync(int id, UpdateContentDto updateContentDto)
        {
            var content = await _context.Contents.FindAsync(id);
            if (content == null)
                return null;

            content.Title = updateContentDto.Title;
            content.Body = updateContentDto.Body;
            content.ContentType = updateContentDto.ContentType;
            content.IsPublished = updateContentDto.IsPublished;
            content.UpdatedAt = DateTime.UtcNow;

            if (updateContentDto.IsPublished && content.PublishedAt == null)
            {
                content.PublishedAt = DateTime.UtcNow;
            }
            else if (!updateContentDto.IsPublished)
            {
                content.PublishedAt = null;
            }

            await _context.SaveChangesAsync();

            return await GetContentByIdAsync(id);
        }

        public async Task<bool> DeleteContentAsync(int id)
        {
            var content = await _context.Contents.FindAsync(id);
            if (content == null)
                return false;

            _context.Contents.Remove(content);
            await _context.SaveChangesAsync();

            return true;
        }

        public async Task<bool> PublishContentAsync(int id)
        {
            var content = await _context.Contents.FindAsync(id);
            if (content == null)
                return false;

            content.IsPublished = true;
            content.PublishedAt = DateTime.UtcNow;
            content.UpdatedAt = DateTime.UtcNow;
            await _context.SaveChangesAsync();

            return true;
        }

        public async Task<bool> UnpublishContentAsync(int id)
        {
            var content = await _context.Contents.FindAsync(id);
            if (content == null)
                return false;

            content.IsPublished = false;
            content.PublishedAt = null;
            content.UpdatedAt = DateTime.UtcNow;
            await _context.SaveChangesAsync();

            return true;
        }
    }
}
